﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace PublicSelfControl
{
    public partial class XtraUserWordControl : DevExpress.XtraEditors.XtraUserControl
    {
        string wordFilepath = null;
        string catchFile = null;
        string catchRtfFile = null;
        bool isOpenWordLooked = false;//word文件是否打开查看

        object Source;
        object Target;

        FileInfo fileinfo;

        int loadStatue = -1;
        public XtraUserWordControl()
        {
            InitializeComponent(); 
        }

        public void LoadWord(string filepath)
        {
            this.wordFilepath = filepath;
            this.Source = filepath;
            this.Target = filepath + ".rtf"; 
            this.fileinfo = new FileInfo(filepath);
            loadStatue = 0;
            


            this.catchFile = fileinfo.DirectoryName + "~$" + fileinfo.Name;
            this.catchRtfFile = fileinfo.DirectoryName + "~$" + fileinfo.Name +".rtf";


            if (fileinfo.Extension.ToUpper().Equals(".DOC"))
            {
                openwordtimer.Start();
            }
            else if (fileinfo.Extension.ToUpper().Equals(".DOCX"))
            {
                richEditControl1.LoadDocument(filepath);
            }
            else 
            {
                MessageBox.Show("错误","非合格的office文档（.doc/.docx）", MessageBoxButtons.OK, MessageBoxIcon.Error  );
            }

        }
        private void openwordtimer_Tick(object sender, EventArgs e)
        { 
            if (isOpenWordLooked)
            { 
                openwordtimer.Stop();
                progressBarControl1.Visible = false;
                return;
            }
            if (!File.Exists(catchFile) && loadStatue == 0) saveAdRtf();
            if (!File.Exists(catchRtfFile) && loadStatue == 2) openRtfToView(); 
           

        }

        /// <summary>
        /// 另存为 RTF临时文件
        /// </summary>
        void saveAdRtf()
        {
            object Unknown = Type.Missing;
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();

            try
            {
                this.loadStatue = 1;
                progressBarControl1.Position = loadStatue;

               

                wordApp.Documents.Open(ref Source, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF;

                wordApp.ActiveDocument.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

               

                this.loadStatue = 2;
                progressBarControl1.Position = loadStatue;
            }
            catch 
            {
                this.loadStatue = 0; 
            }
            finally
            {
                wordApp.Quit(ref Unknown, ref Unknown, ref Unknown);
            }
        }

        /// <summary>
        /// 打开RTF文件
        /// </summary>
        void openRtfToView()
        {
            try
            {
                this.loadStatue = 3; progressBarControl1.Position = loadStatue;

                if (!File.Exists(Target.ToString()))
                {
                    isOpenWordLooked = true;
                    return;
                }
                StreamReader sr = new StreamReader(Target.ToString(), System.Text.Encoding.Default);

                string content = sr.ReadToEnd();

                sr.Close();

                richEditControl1.RtfText = content;

                this.loadStatue = 4; progressBarControl1.Position = loadStatue;


                File.Delete(Target.ToString());//删除临时文件

                isOpenWordLooked = true;
            }
            catch 
            {
                this.loadStatue = 2;
               // MessageBox.Show("加载word出错！" + ex.Message);
            }
        }

      

    }
}
